cap log close
local user "awcassidy1"
use "C:\Users\awcassidy1\Dropbox\jmp_new\cleaned_data/combined_5.dta", clear

la var conditionedsqft "Conditioned Sqft"
la var atticsqft "Attic Sqft"

rename sqft_total sqfttotal
destring sqfttotal, replace
local controls sqfttotal bedstotal bathstotal

local auditvars waterheaterfueltype waterheatertanktype atticrvalue furnacefueltype ///
	eeravg negleakavg ///
	rsavg ductravg negsysageavg ///
	  ductsystem1type sizeavg ///
	progtherm system1locationairhandler system1airhandlertype
	

*make missing sqft if sqft=0
replace sqfttotal=. if sqfttotal==0

la var pricediff_baseline "$\Delta$ Price"

*some sample selection- 5 year window because that's what the ABOR contract said.
replace pricediff=. if lsaleyear<2005 | saleyear>2015

********************************************************************************
*Get incidence of fuel switching.
********************************************************************************
*first figure out type of heat
*it will be nonmissing if they reported at least one of electric or nat gas.
gen ht_electric=0 if regexm(heating, "Electric")>0 | regexm(heating, "Gas")>0
replace ht_electric=1 if regexm(heating, "Electric")>0
gen ht_nat_gas=0 if regexm(heating, "Electric")>0 | regexm(heating, "Gas")>0
replace ht_nat_gas=1 if regexm(heating, "Gas")>0

*valid means they reported only one of nat gas or electric.
gen valid_ht=0
replace valid_ht=1 if ht_electric+ht_nat_gas==1

gen toomany_ht=0
replace toomany_ht=1 if ht_electric+ht_nat_gas>1 & !missing(ht_electric) & !missing(ht_nat_gas)

xtset mlsid relsalenum
gen lht_electric=l.ht_electric if !missing(pricediff)
gen lht_nat_gas=l.ht_nat_gas if !missing(pricediff)

gen lvalid=l.valid_ht if !missing(pricediff)

count if valid_ht==1 & lvalid==1
la var laggedprice "Price at Sale 1"
la var price "Price at Sale 2"
la var lsaleyear "Year of Sale 1"
la var saleyear "Year of Sale 2"

*switched nat gas to electric
count if ht_electric==1 & lht_nat_gas==1 & valid_ht==1 & lvalid==1

*switched electric to nat gas
count if lht_electric==1 & ht_nat_gas==1 & valid_ht==1 & lvalid==1

*stayed electric only
count if ht_electric==1 & lht_electric==1 & valid_ht==1 & lvalid==1

*stayed gas only
count if ht_nat_gas==1 & lht_nat_gas==1 & valid_ht==1 & lvalid==1

*make switcher variable
gen switcher=0 if valid==1 & lvalid==1
replace switcher=1 if lht_electric==1 & ht_nat_gas==1 & valid==1 & lvalid==1
replace switcher=1 if lht_nat_gas==1 & ht_electric==1 & valid==1 & lvalid==1

*how many homes have reported a nat gas or electric heater for both sales?
*this is the denominator.
count if !missing(ht_electric) & !missing(lht_electric) & !missing(pricediff)

*how many reported both for one of the sales
count if (toomany==1 | l.toomany==1) & !missing(pricediff)

la var an_pd "Annualized Price Difference"

merge m:1 saleyear salemonth using "C:\Users/`user'\Dropbox\jmp_new\Austin Electricity and Gas Statistics\elec.dta", nogenerate keep(1 3)


merge m:1 saleyear salemonth using "C:\Users/`user'\Dropbox\jmp_new\Austin Electricity and Gas Statistics\nat gas.dta", nogenerate keep(1 3)

*now adjust it for inflation.
gen texasgas_adj=texasgas*(254.14799/cpiaucsl)
gen austinelec_adj=austinelec*(254.14799/cpiaucsl)

sort mlsid relsalenum
xtset mlsid relsalenum
gen texasgas_adj1=l.texasgas_adj if !missing(pricediff)
gen austinelec_adj1=l.austinelec_adj if !missing(pricediff)

gen texasgas_adj2=texasgas_adj if !missing(pricediff)
gen austinelec_adj2=austinelec_adj if !missing(pricediff)


label var texasgas_adj1 "Price of Gas at Sale 1"
label var texasgas_adj2 "Price of Gas at Sale 2"
label var austinelec_adj1 "Price of Elec at Sale 1"
label var austinelec_adj2 "Price of Elec at Sale 2"


*Divide sale prices by 10
replace price=price/10000
replace list_price=list_price/10000

*make a list price and time on market variable
gen list_pricediff=list_price-l.list_price

*make a variable for transaction minus list price
gen seller_surplus=list_price-price

*seller surplus difference
gen seller_surplus_diff=seller_surplus-l.seller_surplus

replace seller_surplus_diff=. if missing(pricediff)

*make time on market var

gen time_on_market=saled-listd

replace time_on_market=. if time_on_market<0

replace list_pricediff=. if time_on_market<0

gen list_saleyear=year(listd)
gen llist_saleyear=l.list_saleyear

gen log_tom=log(time_on_market)

gen log_time_on_market_diff=log_tom-l.log_tom if !missing(pricediff)


gen time_on_market_diff=time_on_market-l.time_on_market
replace time_on_market_diff=. if missing(pricediff)


replace list_pricediff=. if missing(pricediff)

replace laggedprice=laggedprice/10000

*label lagged price
la var laggedprice "Price at Sale 1"

*hotness variables.
replace hot_sales=hot_sales/1000
replace hot_avg_price_dollars=hot_avg_price_dollars/1000
replace hot_median_price_dollars=hot_median_price_dollars/1000
replace hot_total_listings=hot_total_listings/10000
replace hot_volume_dollars=hot_volume_dollars/10000000


********************************************************************************
*Label Hotness Variables
********************************************************************************
la var hot_sales "Sales, 1,000's"
la var hot_avg_price_dollars "Avg Price"
la var hot_median_price_dollars "Med Price"
la var hot_months_of_inventory "Mos Inventory"
la var hot_total_listings "Tot Listings, 10,000's"
la var hot_volume_dollars "Volume, 10 Mil USD"
la var list_price "List Price"
la var list_pricediff "$\Delta$ List Price"

la var seller_surplus_diff "$\Delta$ List - Sale P"
la var time_on_market_diff "$\Delta$ TOM"

*label year built
la var yearbuiltaudit "Year Built"
la var num_beds_total "Bedrooms"
la var num_baths_total "Bathrooms"

la var seller_surplus "List-Sale Price"


*drop if the sale had ees attachment
drop if ees=="TRUE"

local auditvars waterheaterfueltype waterheatertanktype atticrvalue furnacefueltype ///
	eeravg negleakavg ///
	rsavg ductravg negsysageavg ///
	  ductsystem1type sizeavg ///
	progtherm system1locationairhandler system1airhandlertype

preserve
keep if !missing(pricediff)	
*test to see if there was a big difference in the noncompliers that I do not include in my sample.
eststo clear

eststo: estpost ttest pricediff an_pd  laggedprice price lsaleyear saleyear  ///
	 `auditvars' conditionedsqft sqfttotal num_beds_total ///
	 num_baths_total yearbuiltaudit ///
	 atticsqft austinelec_adj1 austinelec_adj2 texasgas_adj1 texasgas_adj2 ///
	 , by(noncomplier) unequal

esttab using "C:\Users/awcassidy1/Dropbox\jmp_new\tables/bal_noncomplier.tex", ///
				 cells("mu_1(fmt(3)) mu_2(fmt(3)) b(star fmt(4)) p(fmt(3)) N_1(fmt(0)) N_2(fmt(0))") ///
				replace fragment noobs ///
				label booktabs   ///
				collabels(none) nonumber ///
				 unstack wide ///
				 nolines gaps ///
				 star(* 0.10 ** 0.05 *** 0.01)				 
restore




********************************************************************************
*table A10 (noncompliers_std_diffs)
********************************************************************************
eststo clear


mata: mata clear

local auditvars waterheaterfueltype waterheatertanktype atticrvalue furnacefueltype ///
	eeravg negleakavg ///
	rsavg ductravg negsysageavg ///
	  ductsystem1type sizeavg ///
	progtherm system1locationairhandler system1airhandlertype
	
	
local list_to_compare  ///
  ///
an_pd  laggedprice price lsaleyear saleyear  ///
	 conditionedsqft sqfttotal num_beds_total ///
	 num_baths_total yearbuiltaudit ///
	 atticsqft austinelec_adj1 austinelec_adj2 texasgas_adj1 texasgas_adj2
	 
stddiff  `list_to_compare' , ///
	 by(noncomplier)
	 


local words= `: word count `list_to_compare''

mat output=r(output)

mat c_1=output[1..`words',5]

mat li c_1

*second col is variance ratios.
*initialize it.
mat c_2=c_1


forval i=1/`words' {
	mat c_2[`i',1] = (output[`i',2])^2/(output[`i',4])^2
	}
mat li c_1
mat li c_2

mat R3=[c_1, c_2]
mat li R3

ereturn list

eststo clear

estadd matrix R3

esttab e(R3, fmt(%9.3fc)) using "C:\Users\awcassidy1\Dropbox\jmp_new/tables/noncompliers_std_diffs.tex", ///
	label nodepvar replace coeflabels(`e(labels)') nomtitles ///
	collabels(none) ///
	b(%9.3fc) ///
	mlabels(none) eqlabels(none) booktabs fragment
	

********************************************************************************
*get noncomplier dataset for table_11.do
********************************************************************************
	
*output dataset of noncompliers before we drop them.
preserve
	keep if noncomplier==1 
	save "C:\Users\awcassidy1\Dropbox\jmp_new\cleaned_data/noncompliers.dta", replace
restore

drop if noncomplier==1
*only 256 noncomplier situations. Probably exempt.

save "C:\Users\awcassidy1\Dropbox\jmp_new\cleaned_data/combined_6.dta", replace
